
---动作的执行类基类，该类负责该动作的逻辑执行
---@class ActionHandlerBase:GOAPFSMState
---@field action ActionBase 动作
---@field label string  标签
---@field excuteState EActionExcuteState 当前动作状态
---@field agent AgentBase   代理
---@field onFinishCallFunc function 动作完成回调
---@field maps DataMapBase 资源配置
---@field base ActionHandlerBase
ActionHandlerBase = class(GOAPFSMState)
---@param agent AgentBase
---@field maps DataMapBase
---@param action ActionBase
function ActionHandlerBase:ctor(agent, maps, action)
    self.base = ActionHandlerBase;
    self.agent = agent;
    self.action = action;
    self.excuteState = EActionExcuteState.INIT;
    self.maps = maps;
    self.label = action.label;
end

---设置代理状态数据
---@param state State
function ActionHandlerBase:SetAgentStateByOther(state)
    self.agent.agentState:SetByOther(state)
end

---设置代理状态的某数据
---@param key string
---@param value boolean
function ActionHandlerBase:SetAgentState(key, value)
    self.agent.agentState:Set(key,value);
end

---完成动作
function ActionHandlerBase:OnComplete()
    if self.excuteState == EActionExcuteState.EXIT then
        return;
    end
    if self.excuteState ~= EActionExcuteState.EXCUTE then
        --动作完成时会优先更新影响以更新目标，再执行完成回调让计划往下一步执行，若在动作进入状态立马进入完成动作，会导致同一帧更新多个目标的问题，动作完成回调也会被累积
        Debug.LogError("动作不能在非执行阶段完成:"..self.label);
    end

    self.excuteState = EActionExcuteState.EXIT;
    Debug.Log("动作完成："..self.label)
    if self.action.effects ~= nil then
        Debug.Log("设置"..self.label.."影响"..self.action.effects:ToString())
        self:SetAgentStateByOther(self.action.effects);
    end

    if self.onFinishCallFunc ~= nil then
        Debug.Log("执行动作完成后的回调："..self.label);
        self.onFinishCallFunc(self.label);
    end
end

---尝试打断动作,强制完成
function ActionHandlerBase:TryInterruptAction()
    if self.action.interruptible and self.excuteState == EActionExcuteState.EXCUTE then
        Debug.Log("强制打断当前动作:"..self.label)
        self:OnComplete();
        return true
    end
    return false;
end

---能否执行
function ActionHandlerBase:CanPerformAction()
    return self.action:VerifyPreconditions();
end

---设置完成回调
function ActionHandlerBase:SetFinishCallFunc(callFunc)
    self.onFinishCallFunc = callFunc;
end

---获取游戏数据
---@param key string
function ActionHandlerBase:GetGameData(key)
    return self.maps:GetGameData(key);
end

---设置游戏数据
function ActionHandlerBase:SetGameData(key, value)
    self.maps:SetGameData(key,value);
end

---动作进入，子类实现
function ActionHandlerBase:Enter()
    self.excuteState = EActionExcuteState.ENTER;
end

---动作执行，子类实现
function ActionHandlerBase:Execute()
    self.excuteState = EActionExcuteState.EXCUTE;
end

---动作退出，子类实现
function ActionHandlerBase:Exit()
    if self.excuteState ~= EActionExcuteState.EXIT then
        Debug.Log("动作退出，执行完成"..self.label)
        self:OnComplete()
    end
end

function ActionHandlerBase:Release()

end